********************************************************************************
*Political approval ratings and economic performance: evidence from Latin America
*Rodrigo Cerda and Rodrigo Vergara
*
********************************************************************************

clear all
set more off

global path "C:\Users\RCASES\OneDrive - CENTRO DE ESTUDIOS PUBLICOS\CEP\Papers\Papers 2020\Paper - Cerda, Gallardo, Vergara\Actualización de paper\R&R 2021\Archivos definitivos para enviar a revista"

*insert your path here

cd "$path"
use "data.dta"

*Excluding Venezuela

drop if country=="Venezuela"

*Generate variables

encode country, g(country2)
g lgdp=log(gdp_pc_ppp)
label var lgdp "Log (gdp per capita, ppp)"
g lgdp2=lgdp*lgdp
g ltic=log(c_tic_fob)
label var ltic "Log (term of trade)"
label var scandal1 "The number of wrongdoings (corruption, bribery, etc.) per year that directly involve the president." 

*Declare data to be panel data

tsset country2 year
sort country year


*Generate year dummy variables

foreach i in 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2013 2015 2016 2017 2018 {
gen yr`i'=(year==`i')
}

global year "yr2002 yr2003 yr2004 yr2005 yr2006 yr2007 yr2008 yr2009 yr2010 yr2011 yr2013 yr2015 yr2016 yr2017"

//-------
//Table 1
//-------

putexcel set "Table 1", replace
putexcel A1=""
putexcel B1="Observations"
putexcel C1="Mean"
putexcel D1="Standard deviation"
putexcel E1="Minimum"
putexcel F1="Max"

sum gob_aprob
putexcel A2="Complete Sample"
putexcel B2=matrix(r(N))
putexcel C2=matrix(r(mean))
putexcel D2=matrix(r(sd))
putexcel E2=matrix(r(min))
putexcel F2=matrix(r(max))

local i=3
foreach c in Argentina Bolivia Brazil Chile Colombia "Costa Rica" "Dominican Republic" Ecuador "El Salvador" Guatemala Honduras Mexico Nicaragua Panama Paraguay Peru Uruguay {
	sum gob_aprob if country=="`c'"
	putexcel A`i'="`c'"
	putexcel B`i'=matrix(r(N))
	putexcel C`i'=matrix(r(mean))
	putexcel D`i'=matrix(r(sd))
	putexcel E`i'=matrix(r(min))
	putexcel F`i'=matrix(r(max))
	
	local ++i
}


//-------
//Table 2
//-------

preserve 

collapse gob_aprob c_gpub_growth c_gsoc_growth c_gpub_nosoc_growth c_gpubt_pib gdp_crec_cc emp_wb_ilo c_inflation c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst, by(year)

putexcel set "Table 2", replace
putexcel A1=""
putexcel B1="Mean"
putexcel C1="Standard deviation"
putexcel D1="Minimum"
putexcel E1="Max"

local i=2
foreach c in gob_aprob c_gpub_growth c_gsoc_growth c_gpub_nosoc_growth c_gpubt_pib gdp_crec_cc emp_wb_ilo c_inflation c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst {
	sum `c'
	putexcel A`i'="`c'"
	putexcel B`i'=matrix(r(mean))
	putexcel C`i'=matrix(r(sd))
	putexcel D`i'=matrix(r(min))
	putexcel E`i'=matrix(r(max))
	
	local ++i
}

putexcel A2="Government approval"
putexcel A3="Fiscal spending (growth rate)"
putexcel A4="Social fiscal spending (growth rate)"
putexcel A5="Non-social fiscal spending (growth rate)"
putexcel A6="Fiscal spending (% of GDP)"
putexcel A7="GDP growht rate"
putexcel A8="Employment rate (% of total labor force)"
putexcel A9="Inflation rate"
putexcel A10="Term of trade index (2010=100)"
putexcel A11="Inauguration year"
putexcel A12="Scandals per year"
putexcel A13="Victimization rate"
putexcel A14="Distrust in institutions"


restore

//----------------------
//RE and IV regressions
//----------------------

*Table 3

qui xtreg gob_aprob i.year c_gpub_growth, re robust
outreg2 using "Table3", lab word replace  keep(c_gpub_growth) dec(2) addtext(Year Effect, Yes, Country Effect, Yes )
qui xtreg gob_aprob i.year c_gpub_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob, re robust 
outreg2 using "Table3", lab word append  keep(c_gpub_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob) sortvar(c_gpub_growth ) dec(2) addtext(Year Effect, Yes, Country Effect, Yes )
qui xtreg gob_aprob i.year  c_gpub_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst, re robust
outreg2 using "Table3", lab word append  keep(c_gpub_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst) dec(2) addtext(Year Effect, Yes, Country Effect, Yes )
qui xtreg gob_aprob i.year  c_gpubt_pib emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst, re robust 
outreg2 using "Table3", lab word append  keep(c_gpubt_pib emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst) sortvar(c_gpub_growth c_gpubt_pib emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst) dec(2) addtext(Year Effect, Yes, Country Effect, Yes )

*Table 4

qui xtreg gob_aprob i.year  c_gpub_nosoc_growth c_gsoc_growth , re robust
outreg2 using "Table4", lab word replace  keep(c_gpub_nosoc_growth c_gsoc_growth)  dec(2) ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob, re robust
outreg2 using "Table4", lab word append  keep(c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob)  dec(2)  ///
addtext(Year Effect, Yes, Country Effect, Yes )  
qui xtreg gob_aprob i.year c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst, re robust
outreg2 using "Table4", lab word append  keep(c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst)  dec(2) ///
addtext(Year Effect, Yes, Country Effect, Yes )


* Table 5

replace homeless=homeless/1000
replace total_damages=total_damages/1000

xtivreg gob_aprob $year emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst (c_gpub_growth = homeless lgdp lgdp2 total_damages ), first vce(robust) re
xtreg c_gpub_growth homeless total_damages lgdp lgdp2 $year  emp_wb_ilo c_inflation gdp_crec_cc  c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst if e(sample)==1, vce(robust) re
outreg2 using "Table5.doc", lab keep( homeless lgdp lgdp2 total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   replace
xtivreg gob_aprob  $year  emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst (c_gpub_nosoc_growth c_gsoc_growth = homeless lgdp lgdp2 total_damages ), first vce(robust) re
xtreg c_gpub_nosoc_growth homeless total_damages lgdp lgdp2 $year  emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst if e(sample)==1, vce(robust) re
outreg2 using "Table5.doc", lab keep(homeless lgdp lgdp2 total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   append
xtreg c_gsoc_growth homeless total_damages lgdp lgdp2 $year  emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst if e(sample)==1, vce(robust) re
outreg2 using "Table5.doc", lab keep(homeless lgdp lgdp2 total_damages) cttop(First Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(4)   append

*Table 6

sort country2 year

xtivreg gob_aprob $year emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst (c_gpub_growth = homeless lgdp lgdp2 total_damages ), first vce(robust) re
outreg2 using "Table6", lab word keep(emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year c_gpub_growth scandal1 c_vict_rate c_distrust_inst) cttop(Segond Stage) addtext(Year Effect, Yes, Country Effect, Yes )   dec(2)   replace
overid, vceopt(cluster(country2))
underid, jcue
xtivreg gob_aprob  $year  emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst (c_gpub_nosoc_growth c_gsoc_growth = homeless lgdp lgdp2 total_damages ), first vce(robust) re
outreg2 using "Table6", lab word keep(emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year c_gpub_nosoc_growth c_gsoc_growth scandal1 c_vict_rate c_distrust_inst) cttop(Second Stage) addtext(Year Effect, Yes, Country Effect, Yes ) sortvar(c_gpub_growth c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst) dec(2) append
overid, vceopt(cluster(country2))
underid, jcue

//------------------------
// ARELLANO BOND ESTIMATOR
//------------------------

*Table 7

xtabond gob_aprob emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst $year, endogenous(c_gpub_growth, lag(1,.)) vce(robust)
outreg2 using "Table7.doc", lab  cttop(Arellano-Bond) drop ($year) addtext(Year Effect, Yes, Country Effect, Yes )   dec(2)   replace
xtabond gob_aprob emp_wb_ilo c_inflation gdp_crec_cc c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst $year, endogenous(c_gpub_nosoc_growth, lag(1,.)) endogenous(c_gsoc_growth, lag(1,.)) vce(robust)
outreg2 using "Table7.doc", lab  cttop(Arellano-Bond) drop ($year) addtext(Year Effect, Yes, Country Effect, Yes )   sortvar( L.gob_aprob c_gpub_growth c_gpub_nosoc_growth c_gsoc_growth emp_wb_ilo c_inflation gdp_crec c_tic_fob inauguration_year scandal1 c_vict_rate c_distrust_inst) dec(2)   append


//--------
//Figures
//--------

preserve
collapse  gob_aprob gdp_crec_cc c_ti_fob c_gtot_hab c_gtot_pib, by(year)
merge 1:1 year using "Comodity_Index_data"
label var gob_aprob "Gov. approval, %"
label var year "Year"
label var gdp_crec_cc "Growth rate, %"
label var c_ti_fob "Terms-of-trade index (goods, FOB), 2010=100"
label var BCOMIndex "Com. index (total), 2002-18=100"
label var BCOMENIndex "Com. index energy, 2002-18=100"
label var BCOMGRIndex "Com. index grains, 2002-18=100"
label var BCOMINIndex "Com. index industrial metals, 2002-18=100"
label var c_gtot_hab "Per capita social spending, US$ 2010"

*Figure 1
tsline gob_aprob

*Figure 2
twoway tsline gob_aprob , yaxis(2) || tsline gdp_crec_cc , yaxis(1)

*Figure 3
twoway tsline gob_aprob, yaxis(2)  || tsline c_ti_fob , yaxis(1) legend(on order(1 "Gov. approval" 2 "Terms-of-trade index"))

*Figure 4
twoway tsline gob_aprob, yaxis(2)  || tsline BCOMIndex , yaxis(1) legend(on order(1 "Gov. approval" 2 "Com. index (total)"))

*Figure 5

clear 
use "data_latinobarometro_2009.dta"

sort gob_aprob_crisis
graph hbar gob_aprob_crisis, over(country, sort(1) descending )  blabel(total) 
